<template>
  <div>
    <div
      style="
        height: 5vh;
        display: flex;
        justify-content: space-between;
        align-items: center;
      "
    >
      <!-- <div style="font-size: 26px; font-weight: 400; color: $base-color-white">
        {{ title }}！
      </div> -->
      <div>
        <!-- <image fit="contain"  /> -->
        <img
          src="https://devapi.jbkl.com/storage/logo.jpg"
          style="margin-left: 10px"
          width="3%"
        />
      </div>
      <!-- 
      <div style="margin-right: 20px">
        会员 企业邮箱 海外登录 帮助 反馈修复公示
      </div> -->
    </div>
    <div class="register-container">
      <el-row>
        <el-col :lg="14" :md="11" :sm="24" :xl="14" :xs="24">
          <div style="color: transparent">占位符</div>
        </el-col>
        <el-col :lg="9" :md="12" :sm="24" :xl="9" :xs="24">
          <el-form
            ref="registerForm"
            class="register-form"
            :model="form"
            :rules="registerRules"
            size="mini"
          >
            <el-tooltip content="微信扫码更方便" placement="top">
              <img
                class="img"
                src="https://b.yzcdn.cn/public_files/00768c433665a52ff84927cd89702259.png"
                @click="$router.push('/wxlogin')"
              />
            </el-tooltip>
            <div class="title-tips">{{ translateTitle('注册') }}</div>
            <el-form-item prop="real_name">
              <el-input
                v-model.trim="form.real_name"
                v-focus
                auto-complete="off"
                :placeholder="translateTitle('请输入真实姓名')"
                type="text"
              >
                <template #prefix>
                  <vab-icon icon="user-line" />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="auth_tel">
              <el-input
                v-model.trim="form.auth_tel"
                maxlength="11"
                :placeholder="translateTitle('请输入手机号')"
                show-word-limit
                type="text"
              >
                <template #prefix>
                  <vab-icon icon="smartphone-line" />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="sms_code" style="position: relative">
              <el-input
                v-model.trim="form.sms_code"
                :placeholder="translateTitle('请输入手机验证码')"
                type="text"
              >
                <template #prefix>
                  <vab-icon icon="barcode-box-line" />
                </template>
              </el-input>
              <el-button
                class="auth_tel-code"
                :disabled="isGetPhone"
                type="primary"
                @click="getPhoneCode"
              >
                {{ sms_code }}
              </el-button>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model.trim="form.password"
                autocomplete="new-password"
                :placeholder="translateTitle('请输入密码')"
                type="password"
              >
                <template #prefix>
                  <vab-icon icon="lock-line" />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                class="register-btn"
                type="primary"
                @click.native.prevent="handleRegister"
              >
                {{ translateTitle('注册') }}
              </el-button>
              <router-link to="/login">
                <div class="router-to" style="margin-top: 20px">
                  {{ translateTitle('登录') }}
                </div>
              </router-link>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :lg="1" :md="1" :sm="24" :xl="1" :xs="24">
          <div style="color: transparent">占位符</div>
        </el-col>
      </el-row>
    </div>
    <div></div>
  </div>
</template>

<script>
  import { translateTitle } from '@/utils/i18n'
  import { isPassword, isPhone } from '@/utils/validate'
  import { register, getCode } from '@/api/user'
  import { mapActions } from 'vuex'

  export default {
    name: 'Register',
    directives: {
      focus: {
        inserted(el) {
          el.querySelector('input').focus()
        },
      },
    },
    data() {
      const validateUsername = (rule, value, callback) => {
        if ('' === value) {
          callback(new Error(this.translateTitle('真实姓名不能为空')))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (!isPassword(value)) {
          callback(new Error(this.translateTitle('密码不能少于6位')))
        } else {
          callback()
        }
      }
      const validatePhone = (rule, value, callback) => {
        if (!isPhone(value)) {
          callback(new Error(this.translateTitle('请输入正确的手机号')))
        } else {
          callback()
        }
      }
      return {
        isGetPhone: false,
        getPhoneInterval: null,
        sms_code: this.translateTitle('获取验证码'),
        showRegister: false,
        form: {},
        registerRules: {
          real_name: [
            {
              required: true,
              trigger: 'blur',
              message: this.translateTitle('请输入真实姓名'),
            },
            { validator: validateUsername, trigger: 'blur' },
          ],
          auth_tel: [
            {
              required: true,
              trigger: 'blur',
              message: this.translateTitle('请输入手机号'),
            },
            { validator: validatePhone, trigger: 'blur' },
          ],
          password: [
            {
              required: true,
              trigger: 'blur',
              message: this.translateTitle('请输入密码'),
            },
            { validator: validatePassword, trigger: 'blur' },
          ],
          sms_code: [
            {
              required: true,
              trigger: 'blur',
              message: this.translateTitle('请输入手机验证码'),
            },
          ],
        },
        loading: false,
        passwordType: 'password',
      }
    },
    beforeDestroy() {
      this.getPhoneInterval = null
      clearInterval(this.getPhoneInterval)
    },
    methods: {
      ...mapActions({
        setToken: 'user/setToken',
      }),
      translateTitle,
      getPhoneCode() {
        if (!isPhone(this.form.auth_tel)) {
          this.$refs['registerForm'].validateField('auth_tel')
          return
        }
        const data = {
          auth_tel: this.form.auth_tel,
        }

        getCode(data).then(() => {
          this.isGetPhone = true
          let n = 60
          this.getPhoneInterval = setInterval(() => {
            if (n > 0) {
              n--
              this.sms_code = this.translateTitle('获取验证码 ') + n + 's'
            } else {
              clearInterval(this.getPhoneInterval)
              this.sms_code = this.translateTitle('获取验证码')
              this.getPhoneInterval = null
              this.isGetPhone = false
            }
          }, 1000)
        })
      },
      handleRegister() {
        this.$refs['registerForm'].validate(async (valid) => {
          if (valid) {
            const {
              data: { token },
            } = await register(this.form).then((e) => {
              if (e.code == 200) {
                this.$baseConfirm(`注册成功请登录`, null, async () => {
                  await this.setToken(token)
                  await this.$router.push('/select')
                })
              } else {
                this.$message.error(e.message)
              }
            })
            //this.$baseMessage(msg, 'success', 'vab-hey-message-success')
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .register-container {
    height: 85vh;
    background: url('~@/assets/login_images/background.jpg') center center fixed
      no-repeat;
    background-size: cover;

    .register-form {
      border-radius: 6%;
      position: relative;
      max-width: 100%;
      padding: 3.5vh;
      margin: calc((100vh - 590px) / 2) 5vw 5vw;
      overflow: hidden;
      background: #fff;
      /* background: url(/static/img/login_form.73cf0c11.png); */
      background-size: 100% 100%;
      position: relative;

      .img {
        width: 100px;
        height: 100px;
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
      }

      .title-tips {
        margin-top: 29px;
        font-size: 26px;
        font-weight: 400;
        // color: $base-color-white;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .register-btn {
        display: inherit;
        width: 220px;
        height: 30px;
        margin-top: 5px;
        border: 0;

        &:hover {
          opacity: 0.9;
        }
      }

      .auth_tel-code {
        position: absolute;
        top: 8px;
        right: 10px;
        width: 120px;
        height: 32px;
        font-size: 14px;
        color: #fff;
        cursor: pointer;
        user-select: none;
        border-radius: 3px;
      }
    }
    .router-to {
      width: 100px;
      float: left;
    }
    .tips {
      margin-bottom: 10px;
      font-size: $base-font-size-default;
      color: $base-color-white;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    ::v-deep {
      .el-form-item {
        padding-right: 0;
        margin: 20px 0;
        color: #454545;
        background: transparent;
        border: 1px solid transparent;
        border-radius: 2px;

        i {
          position: absolute;
          top: 8px;
          left: 5px;
          z-index: $base-z-index;
          font-size: 16px;
          color: #d7dee3;
          cursor: pointer;
          user-select: none;
        }

        &__content {
          min-height: $base-input-height;
          line-height: $base-input-height;
        }

        &__error {
          position: absolute;
          top: 100%;
          left: 18px;
          font-size: $base-font-size-small;
          line-height: 18px;
          color: $base-color-red;
        }
      }

      .el-input {
        box-sizing: border-box;

        input {
          height: 48px;
          padding-left: 35px;
          font-size: $base-font-size-default;
          line-height: 58px;
          background: #f6f4fc;
          border: 0;
        }
      }
    }
  }
  .vab-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 55px;
    padding: 0 $base-padding 0 $base-padding;
    color: rgba(0, 0, 0, 0.45);
    background: $base-color-white;
    border-top: 1px dashed $base-border-color;

    i {
      margin: 0 5px;
    }
  }
</style>
